package com.guigu.www.tangyingmeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.mapper.BesSmProMapper;
import com.guigu.www.mapper.BespeakPojectMapper;
import com.guigu.www.mapper.BespeakSetMealMapper;
import com.guigu.www.mapper.QuotaMapper;
import com.guigu.www.pojo.*;
import com.guigu.www.tangyingmeng.service.TymBespeakSetMealService;
import com.guigu.www.utils.PDFCreate;
import com.itextpdf.text.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;
@Service
public class TymBespeakSetMealServiceImpl implements TymBespeakSetMealService {

    @Autowired
    private BespeakSetMealMapper setMealMapper;
    @Autowired
    private BespeakPojectMapper pojectMapper;
    @Autowired
    private BesSmProMapper besSmProMapper;
    @Autowired
    private QuotaMapper quotaMapper;

    @Override
    public PageVo<BespeakSetMeal> selsetmealall(Map<String, String> map) {
        QueryWrapper<BespeakSetMeal> wrapper = new QueryWrapper<>();
        wrapper.like("bes_sm_id", map.get("findid")).like("bes_sm_name", map.get("findname")).like("bes_sm_apply", map.get("findsm"));
        IPage<BespeakSetMeal> page = new Page<>(Integer.valueOf(map.get("index")), Integer.valueOf(map.get("pageSize")));
        page = setMealMapper.selectPage(page, wrapper);
        PageVo<BespeakSetMeal> pageVo = new PageVo<>();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(page.getRecords());
        return pageVo;
    }

    @Override
    public List<BespeakPoject> queryPojects() {
        return pojectMapper.selectList(new QueryWrapper<BespeakPoject>().eq("bes_pro_state",0));
    }

    @Override
    public void submitSetMeal(BespeakSetMeal setmeal, Integer[] arr) {
        setMealMapper.insert(setmeal);
        for (Integer integer : arr) {
            BesSmPro pro = new BesSmPro();
            pro.setBesProId(integer);
            pro.setBesSmId(setmeal.getBesSmId());
            besSmProMapper.insert(pro);
        }
    }

    @Override
    public BespeakSetMeal clickUp(Integer besSmId) {
        BespeakSetMeal bespeakSetMeal = setMealMapper.selectById(besSmId);
        List<BespeakPoject> list = pojectMapper.bespeakProject(besSmId);
        bespeakSetMeal.setList(list);
        return bespeakSetMeal;
    }

    @Override
    public void submitUp(BespeakSetMeal setmeal, Integer[] arr) {
        // System.out.println(setmeal);
        setMealMapper.updateById(setmeal);
        besSmProMapper.delete(new QueryWrapper<BesSmPro>().eq("bes_sm_id",setmeal.getBesSmId()));
        for (Integer integer : arr) {
            BesSmPro pro = new BesSmPro();
            pro.setBesProId(integer);
            pro.setBesSmId(setmeal.getBesSmId());
            besSmProMapper.insert(pro);
        }
    }

    @Override
    public void createPDF(String fileName, Integer besSmId) {
        BespeakSetMeal bespeakSetMeal = setMealMapper.selectById(besSmId);
        List<BespeakPoject> list = pojectMapper.bespeakProject(besSmId);
        for (BespeakPoject bespeakPoject : list) {
            Quota quota = quotaMapper.queryByBesProId(bespeakPoject.getBesProId());
            bespeakPoject.setQuota(quota);
        }
        bespeakSetMeal.setList(list);
        // System.out.println(bespeakSetMeal);
        try {
            PDFCreate.createPDF(fileName,bespeakSetMeal);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
